<?php

class OfferModel extends Model {

	//发起悬赏
    public static function newoffer($uid = 0, $tuid = 0, $price = 0, $gb = 0)
	{
        $uid = intval($uid);
        $tuid = intval($tuid);
        $price = intval($price); //悬赏身价不少于1000
        $gb = intval($gb); //悬赏金币不少于1000G
        if (!$uid || !$tuid || $price < 1000 || $gb < 1000) return array('ecode' => ERR302);

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);
		$uext["lv"] = modcall("user", "xp2lv", array($uext["xp"]));
		$ulv = $uext["lv"]; 

		$tuext = daocall("user", "getuserext", array($tuid));
        if (!$tuext || !is_array($tuext)) return array('ecode' => ERR402);
		$tuext["lv"] = modcall("user", "xp2lv", array($tuext["xp"]));
		$tulv = $tuext["lv"];

		if($uext["gb"] < $gb) return array('ecode' => ERR402);

		$uset = array();

		if($gb)
		{
			$uext["gb"] -= $gb;
			$uset["gb"] = $uext["gb"];
		}

		if($uset && !daocall("user", "setuserext", array($uid, $uset))) return array('ecode' => ERR407);

        $ret = &daocall("offer", "newoffer", array($uid, $tuid, array(
			"price"	=> $price,
			"gb"	=> $gb,
			"ctm"	=> time(),
		)));
        if ($ret === false || !is_array($ret)) return array("ecode" => ERR801);
		else return array("ecode" => SUCCESS, 'ret' => &$ret);
    }
	
	//加钱重发悬赏
	public static function upoffer($uid = 0, $tuid = 0, $price = 0, $gb = 0)
	{
        $uid = intval($uid);
        $tuid = intval($tuid);

		$offer = daocall("offer", "getoffer", array($uid, $tuid));
		if(!$offer) return array("ecode" => ERR802);

        $price = intval($price); //悬赏身价不少于1000
        $gb = intval($gb); //悬赏金币不少于1000G

        if (!$uid || !$tuid || $price < 1000 || $gb < 1000 || $price <= $offer["price"] || $gb <= $offer["gb"]) return array('ecode' => ERR302);
		$diffgb = $gb - $offer["gb"];

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);
		$uext["lv"] = modcall("user", "xp2lv", array($uext["xp"]));
		$ulv = $uext["lv"]; 

		$tuext = daocall("user", "getuserext", array($tuid));
        if (!$tuext || !is_array($tuext)) return array('ecode' => ERR402);
		$tuext["lv"] = modcall("user", "xp2lv", array($tuext["xp"]));
		$tulv = $tuext["lv"];

		if($uext["gb"] < $diffgb) return array('ecode' => ERR402);

		$uset = array();

		if($diffgb)
		{
			$uext["gb"] -= $diffgb;
			$uset["gb"] = $uext["gb"];
		}

		if($uset && !daocall("user", "setuserext", array($uid, $uset))) return array('ecode' => ERR407);

        $ret = &daocall("offer", "setoffer", array($uid, $tuid, array(
			"price"	=> $price,
			"gb"	=> $gb,
			"ctm"	=> time(), 
			"ptm" => 0,
			"pcount" => 0,
			"auid" => 0, 
			"actm" => 0,
			"acount" => 0,
			"aperson" => 0,
			"apersonlist" => "",
		)));
        if ($ret === false || !is_array($ret)) return array("ecode" => ERR801);
		else return array("ecode" => SUCCESS, 'ret' => &$ret);
	}

	//接受悬赏
	public static function actoffer($uid = 0, $tuid = 0, $auid = 0)
	{
		$uid = intval($uid);
        $tuid = intval($tuid);
        $auid = intval($auid);

		$offer = daocall("offer", "getoffer", array($uid, $tuid));
		if(!$offer || $offer["auid"] != $auid || (time() - $offer["ptm"]) > 30 * 60) return array("ecode" => ERR802);

		$ret = &daocall("offer", "setoffer", array($uid, $tuid, array(
			"actm" => time(),
			"acount" => 0,
		)));

        if ($ret === false || !is_array($ret)) return array("ecode" => ERR801);
		else return array("ecode" => SUCCESS, 'ret' => &$ret);
	}

	//忽略悬赏
	public static function ignoffer($uid = 0, $tuid = 0, $auid = 0)
	{
		$uid = intval($uid);
        $tuid = intval($tuid);
		$auid = intval($auid);

		$offer = daocall("offer", "getoffer", array($uid, $tuid));
		if(!$offer || $offer["auid"] != $auid || (time() - $offer["ptm"]) > 30 * 60) return array("ecode" => ERR802);

		$ret = &daocall("offer", "setoffer", array($uid, $tuid, array(
			"ptm" => $offer["ptm"] - 60 * 60,
			"auid" => 0,
			"actm" => 0,
		)));

        if ($ret === false || !is_array($ret)) return array("ecode" => ERR801);
		else return array("ecode" => SUCCESS, 'ret' => &$ret);
	}

}
?>